package com.tools.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tools.mapper.user.RoleMapper;
import com.tools.pojo.user.bo.RoleBO;
import com.tools.pojo.user.bo.RoleMenuBO;
import com.tools.pojo.user.dto.RoleDTO;
import com.tools.pojo.user.po.Role;
import com.tools.pojo.user.po.RoleMenu;
import com.tools.pojo.user.vo.RoleVO;
import com.tools.service.user.RoleMenuService;
import com.tools.service.user.RoleService;
import com.tools.utils.BeanUtils;
import io.micrometer.common.util.StringUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    private final RoleMenuService roleMenuService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean createRole(RoleBO roleBO) {
        Role role = new Role();
        BeanUtils.copyProperties(roleBO, role);

        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getName, roleBO.getName());
        Role roleExist = baseMapper.selectOne(queryWrapper);
        if (roleExist != null) {
            throw new IllegalArgumentException("角色名称已存在");
        }
        boolean save = this.save(role);
        if(save){
            RoleMenuBO roleMenuBO = new RoleMenuBO();
            roleMenuBO.setRoleId(role.getId());
            roleMenuBO.setMenuIds(roleBO.getMenuIds());
            roleMenuService.addRoleMenu(roleMenuBO);
            return true;
        }else{
            return false;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteRoleById(Long id) {
        LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleMenu::getRoleId, id);
        boolean delete = this.removeById(id);
        if(delete){
            roleMenuService.remove(queryWrapper);
            return true;
        }else{
            return false;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateRoleStatus(Long id, String status) {
        Role role = baseMapper.selectById(id);
        if (role == null) {
            throw new IllegalArgumentException("角色不存在");
        }
        role.setStatus(status);
        return baseMapper.updateById(role) > 0;
    }

    @Override
    public IPage<RoleVO> getRolePage(RoleDTO roleDTO) {
        IPage<Role> page = new Page<>(roleDTO.getCurrent(), roleDTO.getSize());
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(roleDTO.getRoleType()), Role::getRoleType, roleDTO.getRoleType());
        queryWrapper.like(StringUtils.isNotBlank(roleDTO.getName()), Role::getName, roleDTO.getName());
        queryWrapper.like(StringUtils.isNotBlank(roleDTO.getCode()), Role::getCode, roleDTO.getCode());
        queryWrapper.eq(StringUtils.isNotBlank(roleDTO.getStatus()), Role::getStatus, roleDTO.getStatus());
        queryWrapper.orderByAsc(Role::getSort);
        return baseMapper.selectPage(page, queryWrapper).convert( role -> BeanUtils.toBean(role, RoleVO.class));
    }

    @Override
    public RoleVO getRoleById(Long id) {
        Role role = baseMapper.selectById(id);
        return role != null ? BeanUtils.toBean(role, RoleVO.class) : null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchDeleteRole(List<Long> ids) {
        boolean b = this.removeBatchByIds(ids);
        if(b){
            LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(RoleMenu::getRoleId, ids);
            roleMenuService.remove(queryWrapper);
            return true;
        }else{
            return false;
        }
    }

    @Override
    public List<RoleVO> getRoleSelectList() {
        return baseMapper.selectList(null).stream().map(role -> BeanUtils.toBean(role, RoleVO.class)).collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateRole(RoleBO roleBO) {
        Role role = baseMapper.selectById(roleBO.getId());
        if (role == null) {
            throw new IllegalArgumentException("角色不存在");
        }
        BeanUtils.copyProperties(roleBO, role);
        boolean update = baseMapper.updateById(role) > 0;
        if(update){
            LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(RoleMenu::getRoleId, roleBO.getId());
            roleMenuService.remove(queryWrapper);
            RoleMenuBO roleMenuBO = new RoleMenuBO();
            roleMenuBO.setRoleId(role.getId());
            roleMenuBO.setMenuIds(roleBO.getMenuIds());
            roleMenuService.addRoleMenu(roleMenuBO);
            return true;
        }else{
            return false;
        }
    }
}
